﻿using Futaba.SBus.Readers;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Futaba.SBus.SampleApplication
{
	partial class MainViewModel
	{

		RelayCommand _quitCommand;
		public ICommand QuitCommand
		{
			get
			{
				if (_quitCommand == null)
					_quitCommand = new RelayCommand(ExecuteQuit);
				return _quitCommand;
			}
		}
		private void ExecuteQuit(object parameter)
		{
			Environment.Exit(0);
		}

		RelayCommand _loadSensorConfigCommand;
		public ICommand LoadSensorConfigCommand
		{
			get
			{
				if (_loadSensorConfigCommand == null)
				{
					_loadSensorConfigCommand = new RelayCommand(ExecuteLoadSensorConfig, o => !_parsingService.IsRunning);
					_parsingService.PropertyChanged += (o, e) =>
					{
						if (e.PropertyName == "IsRunning")
							_loadSensorConfigCommand.RaiseCanExecuteChanged();
					};
				}
				return _loadSensorConfigCommand;
			}
		}
		private void ExecuteLoadSensorConfig(object parameter)
		{
			System.Windows.MessageBox.Show("Loading sensor-config...");
		}

		RelayCommand _loadConfigCommand;
		public ICommand LoadConfigCommand
		{
			get
			{
				if (_loadConfigCommand == null)
				{
					_loadConfigCommand = new RelayCommand(ExecuteLoadConfig, o => !_parsingService.IsRunning);
					_parsingService.PropertyChanged += (o, e) =>
					{
						if (e.PropertyName == "IsRunning")
							_loadConfigCommand.RaiseCanExecuteChanged();
					};
				}
				return _loadConfigCommand;
			}
		}
		private void ExecuteLoadConfig(object parameter)
		{
			System.Windows.MessageBox.Show("Loading...");
		}

		RelayCommand _saveConfigCommand;
		public ICommand SaveConfigCommand
		{
			get
			{
				if (_saveConfigCommand == null)
					_saveConfigCommand = new RelayCommand(ExecuteSaveConfig);
				return _saveConfigCommand;
			}
		}
		private void ExecuteSaveConfig(object parameter)
		{
			System.Windows.MessageBox.Show("Saving...");
		}

		RelayCommand _connectCommand;
#pragma warning disable 4014
		public ICommand ConnectCommand
		{
			get
			{
				if (_connectCommand == null)
				{
					_connectCommand = new RelayCommand(o => ExecuteConnectCommand(o), o => SelectedComPort != null || _parsingService.IsRunning);
					_parsingService.PropertyChanged += (o, e) =>
					{
						if (e.PropertyName == "IsRunning")
							_connectCommand.RaiseCanExecuteChanged();
					};
				}
				return _connectCommand;
			}
		}
#pragma warning restore 4014
		private async Task ExecuteConnectCommand(object parameter)
		{
			if (_parsingService.IsRunning)
			{
				await _parsingService.Stop();
				return;
			}
			System.Windows.MessageBox.Show("Connecting");
		}

		RelayCommand _playbackCommand;
		public ICommand PlaybackCommand
		{
			get
			{
				if (_playbackCommand == null)
				{
					_playbackCommand = new RelayCommand(o => ExecutePlaybackCommand(o), o => !_parsingService.IsRunning);
					_parsingService.PropertyChanged += (o, e) =>
					{
						if (e.PropertyName == "IsRunning")
							_playbackCommand.RaiseCanExecuteChanged();
					};
				}
				return _playbackCommand;
			}
		}
		private void ExecutePlaybackCommand(object parameter)
		{
			var filename = GetExistingFile("Select a log-file", "Log files|*.log|All files|*.*");
			if (string.IsNullOrEmpty(filename))
				return;

			var reader = new FileSbusStreamReader(filename);
			_parsingService.Start(reader);
		}

		OpenFileDialog _openFileDialog = new OpenFileDialog();
		private string GetExistingFile(string title, string filter)
		{
			_openFileDialog.CheckFileExists = true;
			_openFileDialog.Filter = filter;
			_openFileDialog.Title = title;
			var success = _openFileDialog.ShowDialog();
			if (!success.GetValueOrDefault())
				return null;
			return _openFileDialog.FileName;
		}

	}
}
